import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shishi/bean/wenda.dart';

import '../Colors.dart';

class WenDapage extends StatefulWidget {
  @override
  _WenDapageState createState() => _WenDapageState();
}

class _WenDapageState extends State<WenDapage> {


  Wenda _wenda;
  List<wendas> _listwenda = new List<wendas>();

  String result = '';

  void GetWenda() async {
    try {

      Response response = await Dio().get(
          "http://api.tianapi.com/txapi/wenda/index?key=a69400da230d90d3d4ed644b6d3d1fb7"
      );

      _wenda = new Wenda(response.data);

      _listwenda.clear();
      _listwenda.addAll(_wenda.newslist);

      setState(() {

      });
    } catch (e) {
      print(e);
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    GetWenda();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('一站到底'),
      ),
      body: Container(
        padding: EdgeInsets.all(5),
        child: Column(
          children: <Widget>[
            Card(
              margin: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.1),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('问题',style: TextStyle(
                    fontFamily: 'kai',
                    fontSize: 30
                  ),),
                  Container(
                    margin: EdgeInsets.all(10),
                    child: Text('${_listwenda[0].quest}',style: TextStyle(
                      fontSize: 20,

                    ),),
                  )
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.1),
              child: TextField(
                onChanged: (v) {
                  result = v;
                },
                decoration: InputDecoration(
                  labelText: '请输入你的答案'
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 40),
              width: MediaQuery.of(context).size.width * 0.8,
              height: MediaQuery.of(context).size.height * 0.07,
              child: RaisedButton(
                shape: StadiumBorder(),
                child: Text('确定',),
                onPressed: () {
                  print(_listwenda[0].result);
                  print(result);
                  if(result != '') {
                    if (_listwenda[0].result.contains(result) && result != '' ) {
                      showDialog(
                          context: context,
                          builder: (context) =>
                              CupertinoAlertDialog(
                                title: Container(
                                  child: Text('回答正确',style: TextStyle(
                                      fontFamily: 'yuwei',
                                      fontSize: 30,
                                      color: Cos.zhutise
                                  ),),
                                ),

                                actions: <Widget>[
                                  FlatButton(
                                    child: Text('返回'),
                                    onPressed: () => Navigator.pop(context),
                                  ),
                                  FlatButton(
                                    child: Text('下一题'),
                                    onPressed: () {
                                      GetWenda();
                                      Navigator.of(context).pop();
                                    },
                                  ),
                                ],
                              ));
                    } else {
                      showDialog(
                          context: context,
                          builder: (context) =>
                              CupertinoAlertDialog(
                                title: Container(
                                  child: Text('回答错误',style: TextStyle(
                                      fontFamily: 'yuwei',
                                      fontSize: 30,
                                      color: Cos.zhutise
                                  ),),
                                ),


                                actions: <Widget>[
                                  FlatButton(
                                    child: Text('返回'),
                                    onPressed: () => Navigator.of(context).pop(),
                                  ),
                                  FlatButton(
                                    child: Text('下一题'),
                                    onPressed: () {
                                      GetWenda();
                                      Navigator.of(context).pop();
                                    },
                                  ),
                                ],
                              ));
                    }
                  }  else {
                    showDialog(
                        context: context,
                        builder: (context) =>
                            CupertinoAlertDialog(
                              title: Container(
                                child: Text('请输入你的答案',style: TextStyle(
                                    fontFamily: 'yuwei',
                                    fontSize: 30,
                                    color: Cos.zhutise
                                ),),
                              ),

                              actions: <Widget>[
                                FlatButton(
                                  child: Text('确定'),
                                  onPressed: () => Navigator.pop(context, true),
                                ),
                              ],
                            ));
                  }
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
